package controllers;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.imageio.ImageIO;

import model.ViewPhotoWithLayerModel;
import view.ViewPhotoWithLayerGUI;
import client.CliMessage;
import client.IObserve;
import client.ImageSerializer;

/**
 * The Class ViewPhotoWithLayerController.
 * @author Dmitry
 */
public class ViewPhotoWithLayerController extends AbstractController implements IObserve {

	/** The view photo with layer gui. */
	private ViewPhotoWithLayerGUI viewPhotoWithLayerGUI;
	
	/** The view photo layer model. */
	private ViewPhotoWithLayerModel viewPhotoLayerModel;
	
	/** The merge. */
	private MergePhoto merge = new MergePhoto();
	
	/**
	 * Instantiates a new view photo with layer controller.
	 *
	 * @param theView the the view
	 * @param theModel the the model
	 */
	public ViewPhotoWithLayerController(ViewPhotoWithLayerGUI theView, ViewPhotoWithLayerModel theModel) {
		super(theView, theModel);
		this.viewPhotoWithLayerGUI = theView;
		this.viewPhotoLayerModel = theModel;
	}

	/**
	 * This method handles incoming photos from the DB.
	 */
public void handlePhoto(CliMessage msgWithTryImage){
		
		ArrayList<HashMap<String,String>> resultToDisplay = msgWithTryImage.getSrs();
		String serializedPhoto ;
		ImageSerializer imageSerializer = new ImageSerializer(""); // null string - we don't save photos on hard drive
		BufferedImage imgToShow;
		BufferedImage layer;
		byte[] output ;
		ByteArrayInputStream bais;
       try {
    	   //Decode the photo
			serializedPhoto = resultToDisplay.get(0).get("PHOTO");
			output = imageSerializer.decodeImage(serializedPhoto);
			bais = new ByteArrayInputStream(output);
			imgToShow = ImageIO.read(bais);
			String layerName = viewPhotoLayerModel.getTheLayer().getLayerName();
			// Decode the Layer
			serializedPhoto = resultToDisplay.get(0).get(layerName);
			output = imageSerializer.decodeImage(serializedPhoto);
			bais = new ByteArrayInputStream(output);
			layer = ImageIO.read(bais);
			viewPhotoWithLayerGUI.displayImage(resizedImage(merge.mergeLayer(imgToShow, layer)));
		} catch (IOException e) {
			e.printStackTrace();
		}
}

	/* (non-Javadoc)
	 * @see client.IObserve#handleDBRequestSuccess()
	 */
	@Override
	public void handleDBRequestSuccess() {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see client.IObserve#handleNullSearchResult()
	 */
	@Override
	public void handleNullSearchResult() {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see client.IObserve#handleDBSearchResult(client.CliMessage)
	 */
	@Override
	public void handleDBSearchResult(CliMessage dbResult) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Resized image.
	 *
	 * @param img - the image requested to be resized.
	 * @return the buffered image - resized image
	 */
	private BufferedImage resizedImage(BufferedImage img){
		BufferedImage newImage = new BufferedImage(viewPhotoWithLayerGUI.getPhotoWithLayerPanelWidth(), viewPhotoWithLayerGUI.getPhotoWithLayerPanelHeight(), BufferedImage.TYPE_INT_RGB);
		Graphics g = newImage.createGraphics();
		g.drawImage(img, 0, 0, viewPhotoWithLayerGUI.getPhotoWithLayerPanelWidth(), viewPhotoWithLayerGUI.getPhotoWithLayerPanelHeight(), null);
		g.dispose();
		return newImage;
	}
}
